import { IsString, IsArray, IsOptional, ValidateNested, IsNumber, Min, Max } from 'class-validator';
import { Type, Transform } from 'class-transformer';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { CreateMessageDto } from './messages.dto';

export class BatchRequestDto {
  @ApiProperty({ description: 'Custom identifier for the request' })
  @IsString()
  custom_id: string;

  @ApiProperty({ type: CreateMessageDto, description: 'Message parameters' })
  @ValidateNested()
  @Type(() => CreateMessageDto)
  params: CreateMessageDto;
}

export class CreateBatchDto {
  @ApiProperty({ type: [BatchRequestDto], description: 'Array of batch requests' })
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => BatchRequestDto)
  requests: BatchRequestDto[];

  @ApiPropertyOptional({ description: 'Batch metadata' })
  @IsOptional()
  metadata?: Record<string, any>;
}

export class RequestCountsDto {
  @ApiProperty({ description: 'Number of requests currently being processed' })
  processing: number;

  @ApiProperty({ description: 'Number of requests that completed successfully' })
  succeeded: number;

  @ApiProperty({ description: 'Number of requests that encountered errors' })
  errored: number;

  @ApiProperty({ description: 'Number of requests that were canceled' })
  canceled: number;

  @ApiProperty({ description: 'Number of requests that expired' })
  expired: number;
}

export class BatchResponseDto {
  @ApiProperty({ description: 'Batch identifier', example: 'msgbatch_013Zva2CMHLNnXjNJJKqJ2EF' })
  id: string;

  @ApiProperty({ enum: ['message_batch'], description: 'Object type' })
  type: 'message_batch';

  @ApiProperty({ 
    enum: ['in_progress', 'canceling', 'ended'], 
    description: 'Current processing status' 
  })
  processing_status: 'in_progress' | 'canceling' | 'ended';

  @ApiProperty({ type: RequestCountsDto, description: 'Request processing counts' })
  request_counts: RequestCountsDto;

  @ApiProperty({ description: 'Batch creation timestamp' })
  created_at: string;

  @ApiProperty({ description: 'Batch expiration timestamp' })
  expires_at: string;

  @ApiProperty({ 
    description: 'Batch completion timestamp', 
    nullable: true 
  })
  ended_at: string | null;

  @ApiProperty({ 
    description: 'URL to download batch results', 
    nullable: true 
  })
  results_url: string | null;

  @ApiPropertyOptional({ description: 'Batch metadata' })
  metadata?: Record<string, any>;
}

export class ListBatchesQueryDto {
  @ApiPropertyOptional({ 
    description: 'Number of batches to return', 
    minimum: 1, 
    maximum: 1000, 
    default: 20 
  })
  @IsOptional()
  @IsNumber()
  @Min(1)
  @Max(1000)
  @Transform(({ value }) => parseInt(value))
  limit?: number = 20;

  @ApiPropertyOptional({ 
    description: 'Cursor for pagination - return results before this ID' 
  })
  @IsOptional()
  @IsString()
  before_id?: string;

  @ApiPropertyOptional({ 
    description: 'Cursor for pagination - return results after this ID' 
  })
  @IsOptional()
  @IsString()
  after_id?: string;
}

export class BatchesResponseDto {
  @ApiProperty({ type: [BatchResponseDto], description: 'Array of batch objects' })
  data: BatchResponseDto[];

  @ApiProperty({ 
    description: 'ID of the first batch in the list', 
    nullable: true 
  })
  first_id: string | null;

  @ApiProperty({ 
    description: 'ID of the last batch in the list', 
    nullable: true 
  })
  last_id: string | null;

  @ApiProperty({ description: 'Whether there are more batches available' })
  has_more: boolean;
}

export class GetBatchParamsDto {
  @ApiProperty({ description: 'Message batch identifier' })
  @IsString()
  message_batch_id: string;
}

export class CancelBatchParamsDto {
  @ApiProperty({ description: 'Message batch identifier' })
  @IsString()
  message_batch_id: string;
}